import Vue from 'vue';
import store from '@/store';
import VueRouter from 'vue-router';
import * as userTypes from '@/store/user/types';

import Layout from '@/pages/layout';

Vue.use(VueRouter);

const router = new VueRouter({
    linkActiveClass: 'active',
    routes: [
        {
            path: '/help',
            name: 'Help',
            component: resolve => require(['@/pages/help/index.vue'], resolve),
            // component: r => require.ensure([], () => r(require('@/pages/help/index.vue')), 'group-foo'),
        }, {
            path: '/login',
            name: 'Login',
            component: resolve => require(['@/pages/login/login.vue'], resolve),
        }, {
            path: '/',
            name: 'Layout',
            component: Layout,
            redirect: 'Index',
            children: [
                {
                    path: '/index',
                    name: 'Index',
                    component: resolve => require(['@/pages/login/login.vue'],
                        resolve),
                },
            ],
        },
    ],
});

/**
 * 全局路由钩子
 */
router.beforeEach((to, from, next) => {
    if (to.fullPath === '/help') {
        next();
        return;
    }
    if (!store.getters[userTypes.USER_INFO].token && to.fullPath !== '/login') {
        Vue.prototype.$alert('请登录系统!', {
            confirmButtonText: '确定',
            callback: action => {
                next('/login');
            },
        });
        return;
    } else if (!!store.getters[userTypes.USER_INFO].token &&
        to.fullPath === '/login') {
        next('/');
        return;
    }
    next();
});
router.afterEach((route) => {});

export default router;
